package ericXie.com.command 
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.greensock.TweenLite;
	import ericXie.com.events.PopUpWindowEvent;
	import ericXie.com.events.SimpleButtonAshyEvent;
	import ericXie.com.model.MainModel;
	import flash.external.ExternalInterface;
	
	/**
	 * ...转盘旋转应用
	 * @author EricXie
	 */
	public class StartRouletteCommand implements ICommand 
	{
		//旋转初始圈数
		private var baseRotate:Number = 1800;
		private var _model:MainModel = MainModel.getInstance();
		//箭头角度
		private var arrowsRotate:Number = 32.5;
		//中奖区域角度
		private var awardAreaRotate:Number = 30;
		//非中奖区域角度
		private var loseAreaRotate:Number = 15;
		//区域数
		private var totalArea:Number = 8;
		//旋转时间
		private var rouletteTime:Number = 10;
		public function StartRouletteCommand() 
		{
			
		}
		
		public function execute(event:CairngormEvent):void 
		{
			var rotate:Number = 0;
			if (Number(_model.getData[1]) != 9) {
				for (var i:uint = 0; i < _model.roulettePrize.length; i++ ) {
					if (_model.roulettePrize[i] == Number(_model.getData[1])) {
						rotate = baseRotate + i * (awardAreaRotate+loseAreaRotate) + (Math.random() * awardAreaRotate) +arrowsRotate;
					}
				}
			}else {
				rotate =baseRotate+ Math.floor(Math.random() * totalArea) * (awardAreaRotate+loseAreaRotate) - (Math.random() * loseAreaRotate)+arrowsRotate;
			}
			//旋转的时候设置初始旋转速度为0
			_model.roulette.rotateSpeed = 1;
			//设置自动旋转为假
			_model.roulette.autoRotation = false;
			//设置开始旋转时中间旋转部分跳转到可旋转时
			_model.halo.startRoulette = false;
			TweenLite.to(_model.roulette._roulette, rouletteTime, { rotation:rotate ,onComplete:onFinshRouletteHandler} );
			TweenLite.to(_model.halo._halo, rouletteTime, { rotation:rotate } );
			
		}
		//旋转完成后，弹出消息框，给JS传输剩余次数
		private function onFinshRouletteHandler():void
		{
			new PopUpWindowEvent(PopUpWindowEvent.POPUP_TYPE).dispatch();
			//if (ExternalInterface.available) {
				//ExternalInterface.call("changeNumeber", String(Number(_model.getData[0]) - 1));
			//}
		}
		
	}

}